package com.lanou.tipsy.fragment.recommend;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.recommend.TodayAlbumAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.entity.recommend.guess.singer_album.AlbumlistEntity;
import com.lanou.tipsy.entity.recommend.guess.singer_album.RecommendAlbumListRoot;
import com.lanou.tipsy.tools.volley.ResponseErrorListener;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by FengJun on 15/10/24.
 * 推荐-猜你喜欢-歌手-专辑列表
 */
public class TodaySingerAlbumFragment extends BaseFragment {
    public static final String TAG = "TodaySingerAlbumFragment";
    private RecyclerView recyclerView;
    private StringRequest request;
    private VolleySingleton volleySingleton;
    private ResponseErrorListener errorListener;
    private AlbumlistEntity entity;
    private ArrayList<AlbumlistEntity> albumlistEntities;
    private TodayAlbumAdapter adapter;
    private Context context;
    private String ting_uid;

    public TodaySingerAlbumFragment(String ting_uid) {
        this.ting_uid = ting_uid;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_recommend_singer_songs, null);
        initView(view);
        analysis();
        return view;
    }

    private void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_singer_songs);
        errorListener = new ResponseErrorListener();
        volleySingleton = VolleySingleton.getInstance();
    }

    //网络拉取数据
    private void analysis() {
        String url = Constants.TODAY_SINGER_ALBUM_START + ting_uid + Constants.TODAY_SINGER_ALBUM_END;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                RecommendAlbumListRoot root = JSON.parseObject(response, RecommendAlbumListRoot.class);
                albumlistEntities = root.getAlbumlist();
                adapter = new TodayAlbumAdapter(context, albumlistEntities);
                Log.i("Test", "-----" + albumlistEntities.get(0).getTitle());
                //设置列表内的信息
                recyclerView.setAdapter(adapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                click();
            }
        }, errorListener);
        volleySingleton.addQueue(request, TAG);
    }

    private void click() {
        adapter.setOnItemClickLitener(new TodayAlbumAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                entity = albumlistEntities.get(position);
                Toast.makeText(context,
                        position + " click, 名字是" + entity.getTitle(),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
        volleySingleton.removeRequest(TAG);
    }
}
